package com.ruoyi.subsidy.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.subsidy.domain.SysPlantingTownttach;
import com.ruoyi.subsidy.domain.bo.SysPlantingTownttachBo;
import com.ruoyi.subsidy.domain.vo.SysPlantingTownttachVo;
import com.ruoyi.subsidy.mapper.SysPlantingTownttachMapper;
import com.ruoyi.subsidy.service.ISysPlantingTownttachService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 乡里请示文件Service业务层处理
 *
 * @author nbacheng
 * @date 2025-08-27
 */
@RequiredArgsConstructor
@Service
public class SysPlantingTownttachServiceImpl implements ISysPlantingTownttachService {

    private final SysPlantingTownttachMapper baseMapper;

    /**
     * 查询乡里请示文件
     */
    @Override
    public SysPlantingTownttachVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询乡里请示文件列表
     */
    @Override
    public TableDataInfo<SysPlantingTownttachVo> queryPageList(SysPlantingTownttachBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysPlantingTownttach> lqw = buildQueryWrapper(bo);
        Page<SysPlantingTownttachVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询乡里请示文件列表
     */
    @Override
    public List<SysPlantingTownttachVo> queryList(SysPlantingTownttachBo bo) {
        LambdaQueryWrapper<SysPlantingTownttach> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysPlantingTownttach> buildQueryWrapper(SysPlantingTownttachBo bo) {
        //Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysPlantingTownttach> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getYear()), SysPlantingTownttach::getYear, bo.getYear());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), SysPlantingTownttach::getType, bo.getType());
        lqw.like(StringUtils.isNotBlank(bo.getName()), SysPlantingTownttach::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getQingshifile()), SysPlantingTownttach::getQingshifile, bo.getQingshifile());
        lqw.eq(StringUtils.isNotBlank(bo.getHechafile()), SysPlantingTownttach::getHechafile, bo.getHechafile());
        lqw.eq(StringUtils.isNotBlank(bo.getSaofile()), SysPlantingTownttach::getSaofile, bo.getSaofile());
        lqw.eq(StringUtils.isNotBlank(bo.getReject()), SysPlantingTownttach::getReject, bo.getReject());
        lqw.eq(StringUtils.isNotBlank(bo.getRejectflag()), SysPlantingTownttach::getRejectflag, bo.getRejectflag());
        lqw.eq(StringUtils.isNotBlank(bo.getIsprice()), SysPlantingTownttach::getIsprice, bo.getIsprice());
        return lqw;
    }

    /**
     * 新增乡里请示文件
     */
    @Override
    public Boolean insertByBo(SysPlantingTownttachBo bo) {
        SysPlantingTownttach add = BeanUtil.toBean(bo, SysPlantingTownttach.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改乡里请示文件
     */
    @Override
    public Boolean updateByBo(SysPlantingTownttachBo bo) {
        SysPlantingTownttach update = BeanUtil.toBean(bo, SysPlantingTownttach.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysPlantingTownttach entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除乡里请示文件
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
